<?php

namespace Admin\Controller;

class ModelController extends AdminController {

    public function index($pid = 0){

        $list = $this['Menu']->get(['pid'=>$pid]);
        //
        int_to_string($list,array('hide'=>array(1=>'是',0=>'否'),'is_dev'=>array(1=>'是',0=>'否')));
        $this->assign('list',$list);
        $this->display();
    }


    public function edit($id = null, $pid = null){
        if ( IS_POST ) {
            $info = $_POST;
            if ( $id = $this['Menu']->update($info) ) {
                $info['id'] = $id;
                // 设置前台提示
                $this->alert['msg'] = '操作成功';
                $this->alert['status'] = 1;
            } else {
                // 设置前台提示
                $this->alert['msg'] = '操作失败 '.$this['Menu']->getError();
                $this->alert['status'] = 0;
            }
            // 前台提示
            $this->alert();

        } else {
            if ( $id ){
                $info = $this['Menu']->get(['id'=>$id])[0];
            } else {
                $info = ['hide'=>0,'is_dev'=>0];
            }
        }

        $menus = $this['Menu']->get();
        $menus = $this['Tree']->toFormatTree($menus);

        $this->assign('menus',$menus);
        $this->assign('info',$info);

        $this->display();
    }

    public function delete($id = null){
        $id = array_unique((array)$id);
        $res = $this['Menu']->remove($id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败 '.$this['Config']->getError());
        }
    }

}